import {
	getPlaylistDetail,
	getSongUrlApi,
	getSongDetailApi,
	getPlaylistComment,
	getCommentApi,
	postCommentApi,
	postLikeComment,
	getLyricApi
} from "@/api/api.js"
import {
	commentMenu
} from "./comment_menu.js"
import {
	myCache
} from "@/utils.js"
export default {
	data() {
		return {
			showSong: false, //展示歌词
			lyric: '', //歌词
			currentTime: 0, //歌词播放时间
			isChange: false, //是否切歌
			currentIndex: 0, //当前音乐的index
			song_detail: null, //初始化歌曲
			audio: null,
			id: 0,
			play_list: [],
			song_list: [],
			top_height: 0,

			avatar: {
				avatarUrl: '',
				nickname: ''
			},
			//评论
			show: false,
			comment_menu: [],
			comments: [],
			hotComments: [],
			topComments: [],
			commentIndex: 0,
			showComments: [], //展示的评论
			pageNo: 1,
			//加载
			showloading: false,
			loadingText: "",
			//发表评论
			comment_content: ""
		}
	},
	watch: {
		//监听选择的评论类目
		commentIndex(val) {
			if (val == 0) {
				this.pageNo = 1;
				this.getComment(1);
			} else if (val == 1) {
				this.pageNo = 1;
				this.getComment(2);
			} else if (val == 2) {
				this.pageNo = 1;
				this.getComment(3);
			}
		}
	},
	onLoad(options) {
		this.id = options.id;
		this.init();
		setTimeout(() => {
			this.getSongUrl();
		}, 500);
	},
	async onReady() {
		this.audio = document.getElementsByTagName("audio");
	},
	methods: {
		async init() {
			let data = {
				id: this.id
			};
			let list = await getPlaylistDetail(data);
			this.play_list = list.playlist;
			this.song_list = this.play_list.tracks;
			this.avatar = {
				avatarUrl: this.play_list.creator.avatarUrl,
				nickname: this.play_list.creator.nickname
			};
			uni.createSelectorQuery().in(this).select('.top-container').boundingClientRect(data => {
				this.top_height = data.height - 15;
			}).exec();
			for (let j = 0; j < this.song_list.length; j++) {
				let artists = "";
				this.song_list[j].ar.map(it => {
					artists += `${it.name}/`;
				});
				this.$set(this.song_list[j], 'my_ar', artists.slice(0, artists.length - 1));
			}

			this.song_detail = this.song_list[0];
			console.log(this.song_list);
		},
		async getSongUrl() {
			let ids = [];
			for (let j = 0; j < this.song_list.length; j++) {
				ids.push(this.song_list[j].id);
			}

			ids = ids.join(',');
			let data = {
				ids
			};
			let res = await getSongUrlApi(data);
			let urls = res.data;
			let result = await getSongDetailApi(data);
			let mvs = result.songs;

			for (let i = 0; i < this.song_list.length; i++) {
				urls.map(it => {
					if (it.id == this.song_list[i].id) {
						this.$set(this.song_list[i], 'song_url', it.url);
					}
				});
			}
		},
		// async getSongDetail(data) {
		// 	let res = await getSongDetailApi(data);
		// },
		formatCount(count) {
			let cnt = Math.round(count / 10000);
			if (cnt >= 1) return cnt + "W";
			else return count;
		},
		//播放音乐
		goAudio(id, index) {
			this.currentIndex = index;
			if (this.song_detail == this.song_list[index]) {
				this.isChange = !this.isChange;
				this.showSong = true;
				this.audio[0].play();
				this.getLyric(id); //歌词
				return;
			}
			this.isChange = !this.isChange;
			this.getLyric(id); //歌词
			this.song_detail = this.song_list[index];
			this.audio[0].autoplay = true;
			this.audio[0].src = this.song_detail.song_url;
			const play = () => {
				this.audio[0].play().catch((err) => {
					console.log(JSON.stringify(err));
					play();
				});
			}
		},
		//播放下一首或上一首
		nextOrPrev(e) {
			if (e == 2) {
				if (this.currentIndex < this.song_list.length - 1) {
					this.currentIndex++;
					this.goAudio(this.song_list[this.currentIndex].id, this.currentIndex);
				} else {
					return;
				}
			} else if (e == 1) {
				if (this.currentIndex > 0) {
					this.currentIndex--;
					this.goAudio(this.song_list[this.currentIndex].id, this.currentIndex);
				} else {
					return;
				}
			}
		},
		//播放mv
		goPlayMv(mv) {
			const url = `/pages/playmv/playmv?id=${mv}`;
			this.$navigateTo(url);
		},
		//菜单点击事件
		showMenu(n) {
			if (n == 2) {
				this.getComment();
			}
		},
		//初始化推荐评论区，获取评论
		async getComment(sortType = 1) {
			this.show = await true;
			this.comment_menu = await commentMenu;
			let obj = {
				id: this.id,
				type: 2,
				sortType,
				pageNo: this.pageNo
			};
			let res = await getCommentApi(obj); //初始化评论区
			let comments = res.data.comments
			this.showComments = comments;
		},
		//界面滑动到底部分页
		async getCommentBottom() {
			this.pageNo++;
			if (this.commentIndex == 0) {
				var sortType = 1;
			} else if (this.commentIndex == 1) {
				var sortType = 2;

			} else if (this.commentIndex == 2) {
				var sortType = 3;
				var cursor = this.showComments[this.showComments.length - 1].time;
			}
			let data = {
				id: this.id,
				type: 2,
				sortType,
				pageNo: this.pageNo,
				cursor: cursor || ''
			};
			let res = await getCommentApi(data);
			let comments = [].concat(this.showComments, res.data.comments);
			this.showComments = comments;

			this.showloading = false;
			this.$forceUpdate();
		},
		//滑动底部
		scrollBottom(e) {
			if (e.detail.direction == 'bottom') {
				this.showloading = true;
				setTimeout(() => {
					this.getCommentBottom();
				}, 1500);
			}
		},
		//弹窗关闭
		popClose() {
			this.commentIndex = 0;
			this.pageNo = 1;
			setTimeout(() => {
				this.show = false;
			}, 10);
		},
		//发表评论
		async postComment(e) {
			this.comment_content = e.detail.value;
			let data = {
				t: 1, //1 发送, 2 回复
				type: 2, // 数字,资源类型
				id: this.id,
				content: this.comment_content,
				cookie: myCache('cookie')
			};
			let res = await postCommentApi(data);
			if (res.code == 200) {
				this.$showToast('发表成功');
				this.comment_content = '';
				this.showComments.unshift(res.comment);
				this.$forceUpdate();
			}
		},
		async delComment(item, index) {
			let data = {
				t: 0, //1 发送, 2 回复, 0删除
				type: 2, // 数字,资源类型
				id: this.id,
				commentId: item.commentId,
				cookie: myCache('cookie')
			};
			let res = await postCommentApi(data);
			if (res.code == 200) {
				this.$showToast('删除成功');
				this.showComments.splice(index, 1);
				this.$forceUpdate();
			}
		},
		//点赞评论
		async likeComment(item) {
			if (item.liked) var t = 0;
			else var t = 1;
			let data = {
				t, //1 为点赞 ,0 为取消点赞
				type: 2, // 数字,资源类型
				id: this.id,
				cid: item.commentId,
				cookie: myCache('cookie')
			};
			let res = await postLikeComment(data);
			if (res.code == 200) {
				item.liked = !item.liked;
				if (item.liked) item.likedCount++;
				else item.likedCount--;
				this.$forceUpdate();
			}
		},
		//获取歌词
		async getLyric(id) {
			let data = {
				id
			};
			let res = await getLyricApi(data);
			this.lyric = res.lrc.lyric;
		},
		//返回
		songBack(e) {
			this.showSong = e;
		},
		//暂停
		pauseMusic(e) {
			if (e) this.audio[0].pause();
			else this.audio[0].play();
		},
		goLyric() {
			this.showSong = !this.showSong;
		},
		//audio timeupdate
		timeupdate(e) {
			this.currentTime = e.detail.currentTime;
		}
	}
}
